<template>
  <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>确认您的订单</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <section>
            <div class="title">
                <p>体检人信息</p>
            </div>
            <table>
                <tr>
                    <td>姓名:</td>
                    <td>{{ order.user.realName }}</td>
                </tr>
                <tr>
                    <td>证件号码:</td>
                    <td>{{ order.user.identityCard }}</td>
                </tr>
                <tr>
                    <td>出生日期:</td>
                    <td>{{ order.user.birthday }}</td>
                </tr>
                <tr>
                    <td>手机号码:</td>
                    <td>{{ order.user.userId }}</td>
                </tr>
            </table>
            <div class="title">
                <p>体检日期</p>
            </div>
            <table>
                <tr>
                    <td>{{ order.selectDay }}</td>
                </tr>
            </table>
            <div class="title">
                <p>体检机构</p>
            </div>
            <table>
                <tr>
                    <td colspan="2">{{ order.hospital.name }}</td>
                </tr>
                <tr>
                    <td>营业时间:</td>
                    <td>{{ order.hospital.businessHours }}</td>
                </tr>
                <tr>
                    <td>采血截止:</td>
                    <td>{{ order.hospital.deadline }}</td>
                </tr>
                <tr>
                    <td>机构电话:</td>
                    <td>{{ order.hospital.telephone }}</td>
                </tr>
                <tr>
                    <td>机构地址:</td>
                    <td>{{ order.hospital.address }}</td>
                </tr>
            </table>
            <div class="title">
                <p>套餐类型</p>
            </div>
            <table>
                <tr>
                    <td>{{ order.setmeal.name }}</td>
                </tr>
            </table>
        </section>
        
        <div ref="alipayForm"></div>
        <div class="bottom-btn">
            <div class="first">实付款: <span>{{ order.setmeal.price }}</span></div>
            <div class="last" @click="confirmOrder">确认支付</div>
        </div>

        <div class="bottom-ban"></div>
        <Footer></Footer>
    </div>
</template>

<script>
import Footer from '@/components/Footer.vue';
import { ref,reactive, toRefs, onMounted } from "vue";
import { useRoute,useRouter } from "vue-router";
import { setSessionStorage,getSessionStorage } from "../common.js";
import axios from "axios";
import router from '@/router';
import Hospital from './Hospital.vue';
axios.defaults.baseURL = "http://localhost:8080/tijian/";

export default {

    setup(){

        const router=useRouter()
        const route=useRoute()

        // let user=getSessionStorage('users')
        // console.log(user)

        // 支付接口
        const alipayForm=ref()

    

      




        const order=reactive({
            user: getSessionStorage('users'),
            selectDay:route.query.selectDay,
            hospital:{
                name: '',
                businessHours: '',
                deadline: '',
                telephone: '',
                address:''
            },
            setmeal:{
                name: '',
                price: ''
            }
        })
        
        

        onMounted(()=>{


            console.log(order.user)
            console.log(order.selectDay)
            
            axios.get('/orders/getSmAndHp',{
                params:{
                    smId: route.query.smId,
                    hpId: route.query.hpId
                }
            }).then((response)=>{
                order.hospital=response.data.data.hospital
                order.setmeal=response.data.data.setmeal
                
            }).catch((error)=>{
                console.log(error)
            })
        })
        
        function confirmOrder(){
            console.log("confirmOrder")
            axios.post('/orders/saveOrders',{
                orderDate:order.selectDay,
                userId:order.user.userId,
                hpId:route.query.hpId,
                smId:route.query.smId
            }).then((response)=>{
                let data=response.data;
                if(data.code==0){
                    console.log(data.data)
                    axios.get('/pay/alipay',{
                        params:{
                            userId: order.user.userId,
                            orderId: data.data.orderId
                        }
                    }).then((res)=>{
                        console.log(res)
                        alipayForm.value.innerHTML=res.data
                        console.log(alipayForm.value);

                        //alipayForm.value.querySelector('form').submit()
                        
                        
                        window.document.forms[0].submit()

                        // window.document.forms[0].addEventListener('submit',function(event){
                        //     event.preventDefault()
                        // })

                        router.push('/appointmentSuccess')

                    }).catch((err)=>{
                        console.log(err)
                    })
                    
                }else{
                    alert(response.data.message)
                }
                
            })

        }
        


        components:{
            Footer
        }
        return{
            onMounted,
            order,
            ...toRefs(order),
            confirmOrder,
            alipayForm
        }
    }

}
</script>

<style scoped>
/*********************** 总容器 ***********************/
.wrapper{
    width: 100%;
    height: 100%;
    background-color: #F9F9F9;
}

/*********************** header ***********************/
header{
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa{
    font-size: 8vw;
}



/*********************** common样式 ***********************/
.top-ban{
    width: 100%;
    height: 15.7vw;
}
.bottom-ban{
    width: 100%;
    height: 26.2vw;
}

/*********************** section ***********************/
section{
    width: 86vw;
    margin: 0 auto;
}
section .title{
    width: 100%;
    height: 12vw;
    border-bottom: solid 1px #EEE;

    display: flex;
    align-items: center;
}
section .title p{
    height: 3.4vw;
    line-height: 3.4vw;
    font-size: 4.2vw;
    font-weight: 600;
    box-sizing: border-box;
    padding-left: 3vw;
    border-left: solid 2px #127A90;
}
section table{
    font-size: 3.6vw;
    color: #555;
    margin-top: 2vw;
}
section table tr{
    line-height: 8vw;
}
section table tr td:first-child{
    width: 22%;
}

/*********************** bottom-btn ***********************/
.bottom-btn{
    width: 100%;
    height: 12vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    bottom: 14.2vw;

    display: flex;
}
.bottom-btn .first{
    flex: 2;
    line-height: 12vw;
    font-size: 4.6vw;
    box-sizing: border-box;
    padding-left: 6vw;
}
.bottom-btn .first span{
    color: #F77B2D;
}
.bottom-btn .last{
    flex: 1;
    background-color: #117C94;
    line-height: 12vw;
    text-align: center;
    font-size: 5vw;
    color: #FFF;

    user-select: none;
    cursor: pointer;
}

</style>